/**
 * 2018年12月23日下午5:15:58
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.HashKit;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.ModelKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.EmployeeDao;
import com.baihang.project.ds.entity.MenuEntity;
import com.baihang.project.ds.model.EmployeeModel;

/**
 * @author whati
 *
 */
@Service
public class EmployeeService extends BaseService {
	@Autowired
	private EmployeeDao dao;
	
	public String getMenuTree() {
		List<MenuEntity>  list =  dao.getMenuEntityList("select * from sys_menu where state=0");
		List<MenuEntity>  list2 = list.stream().filter(p->p.getParentid()==0).collect(Collectors.toList());
		List<MenuEntity>  jsonEntity = new ArrayList<MenuEntity>();
		List<MenuEntity>  children = new ArrayList<MenuEntity>();
		for(MenuEntity  menu : list2) {
			children = list.stream().filter(p->p.getParentid()==menu.getId()).collect(Collectors.toList());
			menu.setChildren(children);
			jsonEntity.add(menu);
		}
		String resultJson = String.format("{\"Rows\":%s}", JsonKit.listToJson(jsonEntity, 3));
		return resultJson;
	}
	
	public String getEmployeeGrid() {
		List<Record> list = dao.queryForRecordList("select * from hr_employee where uid!='admin'");
		String resultJson = String.format("{\"Rows\":%s,\"Total\":%d}", JsonKit.listToJson(list, 1),list.size());
		return resultJson;
	}
	
	public boolean checkEmployeeExist(String userName) {
		EmployeeModel model = dao.getEmployeeModel(String.format("select * from hr_employee where uid='%s' limit 1", userName));
		if(model == null) {
			return true;
		}
		return false;
	}
	
	public boolean saveEmployee(String userName,String realName) {
		EmployeeModel model = new EmployeeModel ();
		model.setAuthorityId(0);
		model.setHouseId(0);
		//model.setIdCard(idCard);
		//model.setMenus(menus);
		model.setName(realName);
		model.setUserName(userName);
		model.setPassword(HashKit.md5("888").toUpperCase());
		//model.setTel(tel);
		int result =dao.save(ModelKit.modelToSaveSql(model));
		if(result>0) {
			return true;
		}
		return false;
	}
	
	public boolean delEmployee(int id) {
		int result = dao.delete(String.format("delete from hr_employee where ID=%d", id));
		if(result>0) {
			return true;
		}
		return false;
	}
	
	public boolean saveEmployeeMenu(String userName,String realName,int id,String menu) {
		int result = dao.update(String.format("update hr_employee set menus='%s',uid='%s',name='%s'  where ID=%d",menu,userName,realName, id));
		if(result>0) {
			return true;
		}
		return false;
	}
	public boolean changePassword(int id,String pwd) {
		int result = dao.update(String.format("update hr_employee set pwd='%s'  where ID=%d",HashKit.md5(pwd).toUpperCase(), id));
		if(result>0) {
			return true;
		}
		return false;
	}
}
